<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="币种" prop="currency">
        <el-select size="mini" v-model="queryParams.currency" @change="getList">
          <el-option
            @click.native="changeCurrency(item)"
            v-for="item in optionList"
            :key="item.currency"
            :label="item.currency"
            :value="item.currency"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="链" prop="chain">
        <el-input
          size="mini"
          v-model="queryParams.chain"
          placeholder="请输入链"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:chain:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="序号" type="index" align="center" min-width="5%"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="链" align="center" prop="chain"/>
      <el-table-column label="别名" align="center" prop="chainName"/>
      <el-form-item label="图标:" prop="cover">
        <template slot-scope="scope">
          <image-preview class="list-img" :src="scope.row.cover"/>
        </template>
      </el-form-item>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:chain:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:chain:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="币种:">
          <el-input v-model="form.currency" :disabled="true"/>
        </el-form-item>
        <el-form-item label="链:" prop="chain">
          <el-input v-model="form.chain" placeholder="请输入链" />
        </el-form-item>
        <el-form-item label="别名:" prop="chainName">
          <el-input v-model="form.chainName" placeholder="请输入别名" />
        </el-form-item>
        <el-form-item label="图标:" prop="cover">
          <ImageUpload v-model="form.cover" :limit="1" :file-size="0.5"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { currencyOptionList,chainPageList,getChainDetail,addChain,editChain,removeChain } from "@/api/triones/interlaceCurrencyChain";

export default {
  name: "data",
  data() {
    return {
      showSearch: true,
      loading: true,
      total: 0,
      dataList: [],
      defaultCurrency: "",
      title: "",
      open: false,
      optionList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        currency: undefined,
        chain: undefined
      },
      form: {},
      rules: {
        chain: [
          { required: true, message: "链不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.defaultCurrency =  this.$route.params.currency;
    this.queryParams.currency = this.$route.params.currency;
    this.getCurrencyList();
    this.getList();
  },
  methods: {
    /** 切换函数 */
    changeCurrency(item) {
      this.queryParams.currency = item.currency;
      this.defaultCurrency = item.currency;
    },
    /** 查询字典类型列表 */
    getCurrencyList() {
      currencyOptionList().then(response => {
        this.optionList = response.data;
      });
    },
    /** 查询字典数据列表 */
    getList() {
      this.loading = true;
      chainPageList(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        currency: this.defaultCurrency,
        chainName: undefined,
        chain: undefined,
        status: "0",
        cover: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.currency = this.defaultCurrency;
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加链";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id
      getChainDetail(id).then(response => {
        this.form = response.data;
        this.form.currency = this.defaultCurrency;
        this.open = true;
        this.title = "修改链";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            this.$modal.confirm('是否确认该操作？').then(() => {
              editChain(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            })
          } else {
            this.$modal.confirm('是否确认该操作？').then(() => {
              addChain(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除链为"' + row.chain + '"的数据项？').then(function() {
        return removeChain(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
